﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace SachOnlineLibrary.DAL
{
    internal class SachDAL
    {
        KetNoiDB ketnoidb;
        internal SachDAL()
        {
            ketnoidb = new KetNoiDB();
        }
        internal DataTable LietKeTatCaSach()
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from Sach";
            return ketnoidb.ExecuteQuery(cmd);
        }
        internal DataTable LietKeSach(int masach)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from Sach where MaSach=@MaSach";
            cmd.Parameters.AddWithValue("@MaSach", masach);
            return ketnoidb.ExecuteQuery(cmd);
        }
        internal bool ThemSach(int maloai, string tensach, string mota, long giatien, int soluong, string tacgia, string nhaxuatban, DateTime ngaycapnhat, string hinh)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "insert into Sach values (@maloai,@tensach,@mota,@giatien,@soluong,@tacgia,@nhaxuatban,@ngaycapnhat,@hinh,1)";
            cmd.Parameters.Add("@maloai", SqlDbType.Int).Value = maloai;
            cmd.Parameters.Add("@tensach", SqlDbType.NVarChar).Value = tensach;
            cmd.Parameters.Add("@mota", SqlDbType.NVarChar).Value = mota;
            cmd.Parameters.Add("@giatien", SqlDbType.BigInt).Value = giatien;
            cmd.Parameters.Add("@soluong", SqlDbType.Int).Value = soluong;
            cmd.Parameters.Add("@tacgia", SqlDbType.NVarChar).Value = tacgia;
            cmd.Parameters.Add("@nhaxuatban", SqlDbType.Int).Value = nhaxuatban;
            cmd.Parameters.Add("@ngaycapnhat", SqlDbType.SmallDateTime).Value = ngaycapnhat;
            cmd.Parameters.Add("@hinh", SqlDbType.NVarChar).Value = hinh;
            return ketnoidb.ExecuteNonQuery(cmd);
        }

        internal bool SuaSach(int masach, int maloai, string tensach, string mota, long giatien, int soluong, string tacgia, string nhaxuatban, DateTime ngaycapnhat, string hinh, bool trangthai)
        {
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "update Sach set maloai=@maloai, nhaxuatban = @nhaxuatban, tensach = @tensach, mota=@mota, giatien = @giatien, soluong= @soluong, tacgia = @tacgia, ngaycapnhat=@ngaycapnhat, hinh = @hinh, trangthai=@trangthai  where masach = @masach";
            cmd.Parameters.Add("@masach", SqlDbType.Int).Value = masach;
            cmd.Parameters.Add("@maloai", SqlDbType.Int).Value = maloai;
            cmd.Parameters.Add("@nhaxuatban", SqlDbType.NVarChar).Value = nhaxuatban;
            cmd.Parameters.Add("@tensach", SqlDbType.NVarChar).Value = tensach;
            cmd.Parameters.Add("@mota", SqlDbType.NVarChar).Value = mota;
            cmd.Parameters.Add("@giatien", SqlDbType.BigInt).Value = giatien;
            cmd.Parameters.Add("@soluong", SqlDbType.Int).Value = soluong;
            cmd.Parameters.Add("@tacgia", SqlDbType.NVarChar).Value = tacgia;
            cmd.Parameters.Add("@ngaycapnhat", SqlDbType.SmallDateTime).Value = ngaycapnhat;
            cmd.Parameters.Add("@hinh", SqlDbType.NVarChar).Value = hinh;
            cmd.Parameters.Add("@trangthai", SqlDbType.Bit).Value = trangthai;
            return ketnoidb.ExecuteNonQuery(cmd);
        }
    }
}
